package myNewProject;

import myNewProject.downloader.Downloader;
import myNewProject.notifier.Notifier;
import myNewProject.parser.Parser;
import myNewProject.repository.Repository;
import myNewProject.utils.CustomResult;

import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.Properties;

public class App {

    public static final Properties CONFIG = new Properties();

    static {
        System.out.println("正在加载配置文件……");
        try {
            CONFIG.load(new FileReader("src/main/java/myNewProject/config_local.properties"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println("配置文件加载成功！");
    }

    public static void main(String[] args) {
        System.out.println("程序开始运行……");
        Downloader downloader = Downloader.setDownloader();
        Parser parser = Parser.setParser();
        Repository repository = Repository.setRepository();
        Notifier notifier = Notifier.setNotifier();

        System.out.println("Downloader正在获取网页源码……");
        String html = String.valueOf(downloader.getSourceCode(CONFIG.getProperty("Url")));
        System.out.println("成功获取网页源码！");

        System.out.println("正在按照要求解析源码……");
        List<CustomResult> parsedResult = parser.parse(html);
        System.out.println("解析完成!（共获取：" + parsedResult.size() + "条数据）");

        repository.outPut(parsedResult);

        System.out.println("按照关键词检索中");
        String[] keywords = CONFIG.getProperty("Keywords").split("_");
        notifier.notice(parsedResult, keywords);
        System.out.println("程序运行结束！");
    }
}
